//
//  CZEmoticonCollectionViewCell.swift
//  Weibo28
//
//  Created by Apple on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//  表情视图cel
class CZEmoticonCollectionViewCell: UICollectionViewCell {
    //  表情数组模型
    var emotions: [CZEmoticon]? {
        didSet {
            
            guard let ets = emotions else {
                return
            }
            //  去除重用问题
            for value in emoticonButtons {
                //  隐藏所有表情按钮
                value.hidden = true
            }
            
            
            //  遍历赋值
            for (i, value) in ets.enumerate() {
                //  取到button
                let button = emoticonButtons[i]
                //  设置表情模型
                button.emoticon = value
//                //  让其显示
//                button.hidden = false
//                if value.type == "0" {
//                    //  如果是图片,则不用设置title
//                    button.setImage(UIImage(named: value.path!), forState: .Normal)
//                    button.setTitle(nil, forState: .Normal)
//                } else {
//                    //  emoji
//                    //  如果是emoji,则不用设置image
//                    button.setImage(nil, forState: .Normal)
//                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
//                }
                
            }
            
            
        }
    }
    
    //  保存表情button
    private lazy var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()
    
    var indexPath: NSIndexPath? {
        didSet {
            guard let index = indexPath else {
                return
            }
            
            messageLabel.text = "当前显示的是第\(index.section)组第\(index.item)行"
        }
    }
    
    //  MARK:   -懒加载视图
    //  显示indexPath label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 30)
        return label
    }()
    
    //  删除按钮
    private lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteEmoticonButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addChildButton()
        contentView.addSubview(deleteEmoticonButton)
//        contentView.addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
        
    }
    //  添加表情控件
    private func addChildButton() {
        //  循环遍历20个表情控件
        for _ in 0..<20 {
            let button = CZEmoticonButton()
            button.addTarget(self, action: "emoticonClick:", forControlEvents: .TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            //  添加到contentview里面
            contentView.addSubview(button)
            //  添加到保存表情数组中
            emoticonButtons.append(button)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //  设置表情按钮的frame
        //  表情按钮的宽度
        let itemWidth = width / 7
        //  表情按钮的高度
        let itemHeight = height / 3
        //  遍历数组 i 代表数组的下标
        for (i, value) in emoticonButtons.enumerate() {
            //  设置按钮的大小
            value.size = CGSize(width: itemWidth, height: itemHeight)
            //  计算col索引
            let colIndex = i % 7
            //  计算row索引
            let rowIndex = i / 7
            //  设置x坐标
            value.x = CGFloat(colIndex) * itemWidth
            //  设置y坐标
            value.y = CGFloat(rowIndex) * itemHeight
            
            
        }
        
        //  设置删除按钮的frame
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeight)
        //  设置x坐标
        deleteEmoticonButton.x = width - itemWidth
        //  设置y坐标
        deleteEmoticonButton.y = height - itemHeight
        
    }
    
    //  MARK:   -- 点击事件
    @objc private func emoticonClick(button: CZEmoticonButton) {
        let emoticon = button.emoticon
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticon)
        
    }
    
    @objc private func deleteEmoticonButtonClick() {
    
        //  点击删除按钮发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonButtonNotification, object: nil)
    }
    
    
}
